package com.pifuke.patient;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.o.app.task.DelayTask;

import com.igexin.sdk.PushManager;
import com.smiier.skin.net.UserLoginTask;
import com.smiier.skin.net.UserLoginTask.UserLoginRequest;
import com.smiier.skin.net.UserLoginTask.UserLoginResponse;
import com.smiier.skin.net.UserThirdLoginTask;
import com.smiier.skin.net.UserThirdLoginTask.UserThirdLoginRequest;
import com.smiier.skin.net.UserThirdLoginTask.UserThirdLoginResponse;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;
import com.umeng.analytics.MobclickAgent;

public class SplashActivity extends BasicActivity {

	protected DelayTask mStopper;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);

		mStopper = new DelayTask(new Runnable() {

			@Override
			public void run() {
				if (GlobalSettings.getShareGuide(getContext())) {
					startActivity(new Intent(getContext(),
							NavigateActivity.class));
				} else {
					startActivity(new Intent(getContext(), GuideActivity.class));
				}
				finish();
			}
		}, 2500);

		int loginType = GlobalSettings.getLoginType(this);
		if (loginType == GlobalSettings.LOGIN_TYPE_USERNAME) {
			login(GlobalSettings.getLoginUserName(this),
					GlobalSettings.getLoginUserPwd(this));
		} else if (loginType == GlobalSettings.LOGIN_TYPE_THIRD) {
			loginThird(GlobalSettings.getLoginThirdType(this),
					GlobalSettings.getLoginThirdId(this));
		}

		mStopper.start();
		PushManager.getInstance().initialize(getApplicationContext());
	}

	@Override
	public boolean onInterceptBackPressed() {
		return true;
	}

	protected void login(String userName, String pwd) {
		UserLoginRequest req = new UserLoginRequest();
		req.mobile = userName;
		req.pwd = pwd;
		UserLoginTask task = new UserLoginTask();
		task.setRequest(req);
		task.addListener(new NetTaskListener<UserLoginTask.UserLoginRequest, UserLoginTask.UserLoginResponse>() {

			@Override
			public void onException(
					INetTask<UserLoginRequest, UserLoginResponse> task,
					Exception e) {

			}

			@Override
			public void onComplete(
					INetTask<UserLoginRequest, UserLoginResponse> task,
					UserLoginResponse response) {
				if (response.ResultCode != 200) {
					return;
				}
				GlobalSettings.sToken = response.Res.Token;
				GlobalSettings.sUid = response.Res.Uid;

				CommonUtility.userGet(getApplicationContext());
			}
		});
		task.setContext(this.getApplicationContext());
		task.start();
	}

	protected void loginThird(String thirdType, String thirdId) {
		UserThirdLoginRequest req = new UserThirdLoginRequest();
		req.third_type = thirdType;
		req.third_id = thirdId;
		UserThirdLoginTask task = new UserThirdLoginTask();
		task.setRequest(req);
		task.addListener(new NetTaskListener<UserThirdLoginTask.UserThirdLoginRequest, UserThirdLoginTask.UserThirdLoginResponse>() {

			@Override
			public void onException(
					INetTask<UserThirdLoginRequest, UserThirdLoginResponse> task,
					Exception e) {

			}

			@Override
			public void onComplete(
					INetTask<UserThirdLoginRequest, UserThirdLoginResponse> task,
					UserThirdLoginResponse response) {
				if (response.ResultCode != 200) {
					return;
				}
				
				Log.i("testtoken","loginThird.response.Res.Token" + response.Res.Token);
				
				GlobalSettings.sToken = response.Res.Token;
				GlobalSettings.sUid = response.Res.Uid;
				CommonUtility.userGet(getApplicationContext());
			}
		});
		task.setContext(this.getApplicationContext());
		task.start();
	}
	
/*	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		
		MobclickAgent.onPause(this);
	}*/

}
